﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class Down : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string filePath = Server.UrlDecode(Request.QueryString["path"]).ToString ();
        string fileName = Server.UrlDecode(Request.QueryString["filename"]).ToString(); 
        //以下程序在page_Load里
        FileStream fs = new FileStream(filePath, FileMode.Open);
        // 设置文件流,filePath为文件路径，你可以通过传参方式传入:download.aspx?path=....
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length); // 读取
        fs.Close();
        Response.ClearContent(); // 清楚缓冲区所有内容
        Response.ClearHeaders(); // 清楚缓冲区所有头
        Response.ContentType = "application/octet-stream"; // 设置输出流的Http MIME类型
        //通知浏览器下载文件而不是打开
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //fileName为需要下载的文件名

        Response.BinaryWrite(bytes); // 写入输入流
        Response.Flush(); // 向客户端发送数据流
        Response.End();
    }

}

